<?php

namespace Core\Data\UDD;

class Field {

    private $_Table;
    private $_DataType;
    private $_KeyType = null;
    private $_Required = false;
    private $_AutoIncrement = false;
    private $_Size = null;

    private $strName;

    function __construct ( Table $objTable, $strName, $constDataType ) {
        $this->_Table = $objTable;
        $this->strName = $strName;
        $this->_DataType = $constDataType;
    }

    function __toString () {
        return (string) $this->strName;
    }

    function __get ( $strName ) {

        switch ( $strName ) {

            case "Table":
                return $this->_Table;

            case "DataType":
                return $this->_DataType;

            case "KeyType":
                return $this->_KeyType;

            case "Required":
                return ( $this->_AutoIncrement or $this->_KeyType == PRIMARY_KEY ) ? true : $this->_Required;

            case "AutoIncrement":
                return $this->_AutoIncrement;

            case "Size":
                return $this->_Size;

        }

    }

    function __set ( $strName, $mixValue ) {

        switch ( $strName ) {

            case "KeyType":
                $this->_KeyType = $mixValue;
                if ( !isset( $this->_AutoIncrement ) ) {
                    $this->_AutoIncrement = true;
                }
                break;

            case "Required":
                if ( is_bool( $mixValue ) ) {
                    $this->_Required = $mixValue;
                } else {
                    throw new \UnexpectedValueException ();
                }
                break;

            case "AutoIncrement":
                if ( is_bool( $mixValue ) ) {
                    $this->_AutoIncrement = $mixValue;
                } else {
                    throw new \UnexpectedValueException ();
                }
                break;

            case "Size":
                if ( is_integer( $mixValue ) ) {
                    $this->_Size = $mixValue;
                } else {
                    throw new \UnexpectedValueException ();
                }
                break;

        }

    }

}

?>
